home *** CD-ROM | disk | FTP | other *** search
- MODULE pascal;
-
- <<
- The Triangle of Pascal.
-
- The result of this program is a real triangle of Pascal of height
- Height, not just a list representing such a triangle:
-
- 1
- 1 1
- 1 2 1
- 1 3 3 1
- 1 4 6 4 1 etc.
- >>
-
- IMPORT deltaI, deltaIO;
-
-
- TYPE
-
- << A row of the triangle is represented by a list of integers,
- the triangle as a list of rows:
- >>
- :: Row -> [INT];
- :: Triangle -> [Row];
-
-
- MACRO
-
- == Some constants
-
- NrRows -> 18; == Number of rows to be shown.
- Middle -> 40; == The middle of a 80 character line.
-
-
- RULE
-
- == Miscellaneous functions
-
- :: AddList [INT] [INT] -> [INT];
- AddList [a|x] [b|y] -> [+ a b | AddList x y];
- AddList [] [] -> [];
-
- :: Append [INT] INT -> [INT];
- Append [a|r] x -> [a | Append r x];
- Append [] x -> [x];
-
- :: Take INT [x] -> [x];
- Take 0 list -> [];
- Take n [h|t] -> [h | Take (-- n) t];
- Take n [] -> [];
-
- :: FPutInt INT FILE -> FILE;
- FPutInt n out -> FPutS (ITOS n) out;
-
- :: NrOfDigits INT -> INT;
- NrOfDigits 0 -> 0;
- NrOfDigits n -> ++ (NrOfDigits (/ n 10));
-
-
- == Calculating the Triangle.
-
- :: Pascal -> Triangle;
- Pascal -> p: [[1] | MapNext p];
-
- :: MapNext Triangle -> Triangle;
- MapNext [a|r] -> [Next a | MapNext r];
- MapNext [] -> [];
-
- :: Next Row -> Row;
- Next x -> AddList [0|x] (Append x 0);
-
-
- == Formatting the list representing the triangle as a real triangle.
-
- :: FormatRows Triangle FILE -> FILE;
- FormatRows [f|r] out -> FormatRows r (FPutC '\n' (FormatRow f out));
- FormatRows [] out -> out;
-
- :: FormatRow Row FILE -> FILE;
- FormatRow row out
- -> FormatElems row (Spaces (- Middle (/ (Length row) 2)) out);
-
- :: FormatElems Row FILE -> FILE;
- FormatElems [f|r] out -> FormatElems r (FPutInt f (FPutC ' ' out));
- FormatElems [] out -> out;
-
- :: Spaces INT FILE -> FILE;
- Spaces n out -> out , IF <= n 0
- -> Spaces (-- n) (FPutC ' ' out);
-
- :: Length Row -> INT;
- Length [f|r] -> ++ (+ (NrOfDigits f) (Length r));
- Length [] -> -1;
-
-
- << The Start rule: The first NrRows rows of the (infinite) triangle
- returned by Pascal are taken and shown on the screen (StdIO) as a
- triangle by means of FormatRows.
- >>
- :: Start -> FILE;
- Start -> FormatRows (Take NrRows Pascal) StdIO;
-